# -*- coding: utf-8 -*-
from django.db import models
from django.contrib.localflavor.br.br_states import STATE_CHOICES

PESSOA_CHOICES = (
    ('fisi', 'Física'),
    ('juri', 'Jurídica')
)

SEXO_CHOICES = (
    ('masc', 'Maculino'),
    ('femi', 'Feminino')
)

TIPO_CHOICES = (
    ('resi', 'Residencial'),
    ('come', 'Comercial')
)

class Pessoa(models.Model):
    tipo = models.CharField(max_length=4, choices=PESSOA_CHOICES)
    email = models.EmailField(max_length=80, verbose_name='E-mail', unique=True)
    senha = models.CharField(max_length=50)
    
    def __unicode__(self):
        return u"%s" % self.email

class Fisica(Pessoa):
    nome = models.CharField(
        max_length=100
        )
    cpf = models.CharField(
        max_length=20,
        unique=True,
        help_text='Formato: XXX.XXX.XXX-XX',
        verbose_name='CPF'
        )
    rg = models.CharField(
        max_length=20,
        verbose_name='RG',
        null=True,
        blank=True
        )
    nascimento = models.DateField(
        verbose_name='Data de nascimento',
        help_text='Formato: XX/XX/XXXX'
        )
    celular = models.CharField(
        max_length=15,
        null=True,
        blank=True,
        help_text='Formato: XX-XXXX-XXXX'
        )
    
class Juridica(Pessoa):
    razao_social = models.CharField(
        verbose_name='Razão social',
        max_length=100
        )
    nome_fantasia = models.CharField(
        verbose_name='Nome fantasia',
        max_length=100,
        null=True,
        blank=True
        )
    cnpj = models.CharField(
        verbose_name='C.N.P.J',
        max_length=25,
        help_text='Formato: XX.XXX.XXX/XXXX-XX'
        )
    ie = models.CharField(
        verbose_name='Inscrição Estadual',
        max_length=25,
        null=True,
        blank=True
        )
    nome_responsavel = models.CharField(
        verbose_name='Nome do reponsável',
        max_length=100
        )
    celular_responsavel = models.CharField(
        verbose_name='Telefone do resposável',
        max_length=100,
        null=True,
        blank=True,
        help_text='Formato: XX-XXXX-XXXX'
        )
    
    def __unicode__(self):
        return "%s (%s)" % (self.razao_social, self.cnpj)
        
class Endereco(models.Model):
    pessoa = models.ForeignKey(Pessoa, verbose_name='Usuário')
    cep = models.CharField(
        max_length=10,
        verbose_name='CEP',
        help_text='Formato: XXXXX-XXX'
        )
    tipo = models.CharField(
        max_length=4,
        choices=TIPO_CHOICES,
        verbose_name='Tipo'
        )
    endereco = models.CharField(max_length=100, verbose_name='Endereço')
    numero = models.CharField(max_length=20, verbose_name='Número')
    complemento = models.CharField(max_length=50, null=True, blank=True)
    bairro = models.CharField(max_length=80)
    cidade = models.CharField(max_length=80)
    uf = models.CharField(
        max_length=2,
        choices=STATE_CHOICES,
        verbose_name='UF'
        )
    telefone1 = models.CharField(
        max_length=15,
        verbose_name='Telefone 1',
        help_text='Formato: XX-XXXX-XXXX'
        )
    telefone2 = models.CharField(
        max_length=15,
        null=True,
        blank=True,
        verbose_name='Telefone 2',
        help_text='Formato: XX-XXXX-XXXX'
        )
    referencia = models.CharField(
        max_length=150,
        verbose_name='Referência',
        null=True,
        blank=True
        )
    padrao = models.NullBooleanField(verbose_name='Endereço padrão')
    
    def __unicode__(self):
        return "%s, %s - %s - %s" % (self.endereco, self.numero, self.cidade, \
                                     self.uf)
